//
//  Quartz2DViewController.m
//  Animation
//
//  Created by Angela on 16/8/11.
//  Copyright © 2016年 Angela. All rights reserved.
//信纸条纹

#import "Quartz2DViewController.h"

@interface Quartz2DViewController ()<UITextViewDelegate,UIGestureRecognizerDelegate>
@property (strong , nonatomic) UITextView *textview;//
@property (nonatomic , strong) UIButton *perBtn;//
@property (nonatomic , strong) UIButton *nextBtn;//
@property (nonatomic , strong) UILabel *pageLabe;//
@property (nonatomic , strong) CATransition *ca;
@property (nonatomic , assign) int index;
@end
@implementation Quartz2DViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self testLineUser];
}
#pragma mark-信纸使用
-(void)testLineUser{
    // 1.生成一张以后用于平铺的小图片
    CGSize size = CGSizeMake(self.view.frame.size.width, 26);
    UIGraphicsBeginImageContextWithOptions(size , NO, 0);
    
    // 2.画矩形
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGFloat height = 26;
    CGContextAddRect(ctx, CGRectMake(0, 0, self.view.frame.size.width, height));
    [[UIColor yellowColor] set];
    CGContextFillPath(ctx);
    
    // 3.画线条
    
    CGFloat lineWidth = 1;
    CGFloat lineY = height - lineWidth;
    CGFloat lineX = 0;
    CGContextMoveToPoint(ctx, lineX, lineY);
    CGContextAddLineToPoint(ctx, [UIScreen mainScreen].bounds.size.width, lineY);
    [[UIColor blackColor] set];
    CGContextStrokePath(ctx);
    
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIColor *color=[UIColor colorWithPatternImage:image];
    self.index=1;
    self.textview=[[UITextView alloc]initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-50)];
    self.textview.delegate=self;
    self.textview.font=[UIFont systemFontOfSize:17];
    self.textview.alwaysBounceVertical=NO;
    self.textview.alwaysBounceHorizontal=NO;
    self.textview.bounces=NO;
    self.textview.backgroundColor=color;
    [self.view addSubview:self.textview];
    
    _perBtn=[[UIButton alloc]initWithFrame:CGRectMake(10,self.textview.frame.size.height+10, 100, 40)];
    [_perBtn setTitle:@"上一页" forState:UIControlStateNormal];
    [_perBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_perBtn addTarget:self action:@selector(perBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_perBtn];
    
    _nextBtn=[[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-120,self.textview.frame.size.height+10, 100, 40)];
    [_nextBtn setTitle:@"下一页" forState:UIControlStateNormal];
    [_nextBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_nextBtn];
    
  
    _pageLabe=[[UILabel alloc]initWithFrame:CGRectMake(120, self.textview.frame.size.height+10,80, 40)];
    _pageLabe.font=[UIFont systemFontOfSize:16];
    _pageLabe.textAlignment=NSTextAlignmentCenter;
    _pageLabe.text=@"第1页";
    [self.view addSubview:_pageLabe];
    [self addSwipeGestureRecognizer];
}
-(void)textViewDidChange:(UITextView *)textView{
    //    textview 改变字体的行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 8;// 字体的行间距
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:15],
                                 
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 
                                 };
    
    textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
}
- (void)perBtnClick:(UIButton *)sender {
    if (self.index>1) {
        self.index--;
        self.pageLabe.text=[NSString stringWithFormat:@"第%d页",self.index];
        _ca = [[CATransition alloc] init];
        _ca.type = @"pageCurl";
        [self.textview.layer addAnimation:_ca forKey:nil];
    }else{
        return;
    }
}
- (void)nextBtnClick:(UIButton *)sender {
    self.index++;
    self.pageLabe.text=[NSString stringWithFormat:@"第%d页",self.index];
    _ca = [[CATransition alloc] init];
    _ca.type = @"pageCurl";
    [self.textview.layer addAnimation:_ca forKey:nil];
}

/**
 手势事件处理
 */
- (void)swipe:(UISwipeGestureRecognizer *)gesture
{    //判断方向
    //向右滑
    if (gesture.direction == UISwipeGestureRecognizerDirectionRight)
    {
        if (self.index>1) {
            self.index--;
            self.pageLabe.text=[NSString stringWithFormat:@"第%d页",self.index];
            _ca = [[CATransition alloc] init];
            _ca.type = @"pageCurl";
            _ca.subtype=kCATransitionFromLeft;
            [self.textview.layer addAnimation:_ca forKey:nil];
        }else{
            return;
        }
    }
    else
    {
        self.index++;
        self.pageLabe.text=[NSString stringWithFormat:@"第%d页",self.index];
        _ca = [[CATransition alloc] init];
        _ca.type = @"pageCurl";
        _ca.subtype=kCATransitionFromRight;
        [self.textview.layer addAnimation:_ca forKey:nil];
    }
}

//添加轻扫手势
- (void)addSwipeGestureRecognizer
{
    UISwipeGestureRecognizer *swipeForLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    //方向
    swipeForLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.textview addGestureRecognizer:swipeForLeft];
    
    UISwipeGestureRecognizer *swipeForRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    swipeForRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.textview addGestureRecognizer:swipeForRight];
}
#pragma mark-图片平铺整个界面
-(void)testPuctureTile{
    UIImage *image=[UIImage imageNamed:@"180X180.png"];
    UIColor *color=[UIColor colorWithPatternImage:image];
    self.view.backgroundColor=color;
}
#pragma mark-信纸条纹
-(void)testLineTile{
    // 1.生成一张以后用于平铺的小图片
    CGSize size = CGSizeMake(self.view.frame.size.width, 35);
    UIGraphicsBeginImageContextWithOptions(size , NO, 0);
    
    // 2.画矩形
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGFloat height = 35;
    CGContextAddRect(ctx, CGRectMake(0, 0, self.view.frame.size.width, height));
    [[UIColor whiteColor] set];
    CGContextFillPath(ctx);
    
    // 3.画线条
    
    CGFloat lineWidth = 2;
    CGFloat lineY = height - lineWidth;
    CGFloat lineX = 0;
    CGContextMoveToPoint(ctx, lineX, lineY);
    CGContextAddLineToPoint(ctx, [UIScreen mainScreen].bounds.size.width, lineY);
    [[UIColor blackColor] set];
    CGContextStrokePath(ctx);
    
    
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIColor *color=[UIColor colorWithPatternImage:image];
    self.view.backgroundColor=color;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
